package com.zc.operator.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.UUID;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.zc.operator.domain.ChargeStationPrice;
import com.zc.operator.mapper.ChargeStationPriceTypeMapper;
import com.zc.operator.domain.ChargeStationPriceType;
import com.zc.operator.service.IChargeStationPriceTypeService;

import static com.ruoyi.common.utils.SecurityUtils.getDeptId;
import static com.ruoyi.common.utils.SecurityUtils.getUsername;

/**
 * 价格类型Service业务层处理
 *
 * @author glb
 * @date 2023-05-25
 */
@Service
public class ChargeStationPriceTypeServiceImpl implements IChargeStationPriceTypeService
{
    @Autowired
    private ChargeStationPriceTypeMapper chargeStationPriceTypeMapper;

    /**
     * 查询价格类型
     *
     * @param priceTypeId 价格类型主键
     * @return 价格类型
     */
    @Override
    public ChargeStationPriceType selectChargeStationPriceTypeByPriceTypeId(Long priceTypeId)
    {
        return chargeStationPriceTypeMapper.selectChargeStationPriceTypeByPriceTypeId(priceTypeId);
    }

    /**
     * 查询价格类型列表
     *
     * @param chargeStationPriceType 价格类型
     * @return 价格类型
     */
    @Override
    public List<ChargeStationPriceType> selectChargeStationPriceTypeList(ChargeStationPriceType chargeStationPriceType)
    {
        return chargeStationPriceTypeMapper.selectChargeStationPriceTypeList(chargeStationPriceType);
    }

    /**
     * 新增价格类型
     *
     * @param chargeStationPriceType 价格类型
     * @return 结果
     */
    @Transactional
    @Override
    public int insertChargeStationPriceType(ChargeStationPriceType chargeStationPriceType)
    {
        chargeStationPriceType.setPriceTypeCode(UUID.randomUUID().toString().replace("-", ""));
        chargeStationPriceType.setDeptId(getDeptId());
        chargeStationPriceType.setCreateBy(getUsername());
        chargeStationPriceType.setCreateTime(DateUtils.getNowDate());
        int rows = chargeStationPriceTypeMapper.insertChargeStationPriceType(chargeStationPriceType);
        insertChargeStationPrice(chargeStationPriceType);
        return rows;
    }

    /**
     * 修改价格类型
     *
     * @param chargeStationPriceType 价格类型
     * @return 结果
     */
    @Transactional
    @Override
    public int updateChargeStationPriceType(ChargeStationPriceType chargeStationPriceType)
    {
        chargeStationPriceType.setUpdateBy(getUsername());
        chargeStationPriceType.setUpdateTime(DateUtils.getNowDate());
        chargeStationPriceTypeMapper.deleteChargeStationPriceByPriceTypeCode(chargeStationPriceType.getPriceTypeCode());
        insertChargeStationPrice(chargeStationPriceType);
        return chargeStationPriceTypeMapper.updateChargeStationPriceType(chargeStationPriceType);
    }

    /**
     * 批量删除价格类型
     *
     * @param priceTypeIds 需要删除的价格类型主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteChargeStationPriceTypeByPriceTypeIds(Long[] priceTypeIds)
    {
        chargeStationPriceTypeMapper.deleteChargeStationPriceByPriceTypeIds(priceTypeIds);
        return chargeStationPriceTypeMapper.deleteChargeStationPriceTypeByPriceTypeIds(priceTypeIds);
    }

    /**
     * 删除价格类型信息
     *
     * @param priceTypeId 价格类型主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteChargeStationPriceTypeByPriceTypeId(Long priceTypeId)
    {
        chargeStationPriceTypeMapper.deleteChargeStationPriceByPriceTypeId(priceTypeId);
        return chargeStationPriceTypeMapper.deleteChargeStationPriceTypeByPriceTypeId(priceTypeId);
    }

    /**
     * 新增场站电区间价设置信息
     *
     * @param chargeStationPriceType 价格类型对象
     */
    public void insertChargeStationPrice(ChargeStationPriceType chargeStationPriceType)
    {
        List<ChargeStationPrice> chargeStationPriceList = chargeStationPriceType.getChargeStationPriceList();
        Long priceTypeId = chargeStationPriceType.getPriceTypeId();
        String priceTypeCode = chargeStationPriceType.getPriceTypeCode();
        if (StringUtils.isNotNull(chargeStationPriceList))
        {
            List<ChargeStationPrice> list = new ArrayList<ChargeStationPrice>();
            for (ChargeStationPrice chargeStationPrice : chargeStationPriceList)
            {
                chargeStationPrice.setPriceTypeCode(priceTypeCode);
                list.add(chargeStationPrice);
            }
            if (list.size() > 0)
            {
                chargeStationPriceTypeMapper.batchChargeStationPrice(list);
            }
        }
    }
}
